#pragma once
#include <iostream>
#include <cstring>
#include<cassert>
namespace aoqiang
{
    static const int initNum = 4;
    template <class T>
    class vector
    {
        typedef T *iterator;
        typedef const T *const_iterator;

    public:
        iterator begin()
        {
            return start_;
        }
        iterator end()
        {
            return finish_;
        }
        const_iterator begin() const
        {
            return start_;
        }
        const_iterator end() const
        {
            return finish_;
        }
        T &operator[](size_t pos)
        {
            assert(pos < size());
            return start_[pos];
        }
        //构造
        vector() : start_(nullptr), finish_(nullptr), endOfStorage_(nullptr)
        {
        }
        //拷贝构造
        vector(const vector<T>& v)
        {
            start_ = new T[v.capacity()];
            // for(size_t i = 0;i < v.size();++i)
            // {
            //     start_[i] = v.start_[i];
            // }
            memcpy(start_,v.start_,sizeof(T) * v.size());
            finish_ = start_ + v.size();
            endOfStorage_ = start_ + v.capacity();


        }
        void push_back(const T &t)
        {
            // 判断是否需要扩容
            if (finish_ == endOfStorage_)
            {
                size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newcapacity);
            }
            //扩容完毕，

            *finish_ = t;
            finish_++;
        }
        iterator insert(iterator pos, const T &t)
        {
            assert(pos >= start_ && pos <= finish_);
            if (finish_ == endOfStorage_)
            {
                size_t len = pos - start_;
                size_t newcapacity = capacity() == 0 ? 4 : capacity() * 2;
                reserve(newcapacity);

                pos = start_ + len;
            }

            iterator end = finish_ - 1;

            while (end >= pos)
            {
                *(end + 1) = *end;
                --end;
            }
            *pos = t;
            ++finish_;
            return pos;
        }
        iterator find(const T& t)
        {
            for(auto iter = begin();iter != end();++iter)
            {
                if(*iter == t)
                {
                    return iter;
                }
            }
            return end();
        }
        
        void reserve(size_t n)
        {
            if (n > capacity())
            {
                // 开辟空间
                size_t sz = size();
                T *tmp = new T[n];
                if (start_)
                {
                    memcpy(tmp, start_, sizeof(T) * sz);
                    delete[] start_;
                }
                start_ = tmp;
                finish_ = start_ + sz;
                endOfStorage_ = start_ + n;
            }
        }
        void resize(size_t n,const T& val = T())
        {
            if(n < size())
            {
                finish_ = start_ + n;
            }
            else
            {
                reserve(n);
                while(finish_ != start_ + n)
                {
                    *finish_ = val;
                    ++finish_;
                }
            }
        }
        void swap(vector<T>& t)
        {
            std::swap(start_,t.start_);
            std::swap(finish_,t.finish_);
            std::swap(endOfStorage_,t.endOfStorage_);
        }
        vector<T> & operator=(const vector<T>& t)
        {
            swap(v);
            return *this;
        }
        void pop_back()
        {
            finish_--;
        }
        size_t capacity()const
        {
            return endOfStorage_ - start_;
        }
        size_t size()const
        {
            return finish_ - start_;
        }
        ~vector()
        {
            if(start_)
            {
                delete[] start_;
                start_ = nullptr;
                finish_ = endOfStorage_ = nullptr;
            }
        }

    private:
        T *start_;
        T *finish_;
        T *endOfStorage_;
    };

    void print(const vector<int> &v)
    {
        for (auto e : v)
        {
            std::cout << e << " ";
        }
        std::cout << std::endl;
    }

}